<?php
/**
 * 用户控制器
 * @file ctrl/menu.ctrl.php
 * @author dawei 20130802
 * @version 1.0
 */
class MenuCtrl extends BaseCtrl implements Menu{
    /**
     * 是否开启调试模式json
     * @var array
     */
    private $_isJson = true;
    /**
     * 存储数据返回的临时数据
     * @var ALL
     */
    private $_re = '';
    /**
     * 安全字段
     * @var array
     */
    private $_fields = array('mid','name','content','present_id','rid','display','create_time','is_show','description','level');
    /**
     * 传来的所有参数
     * @var array
     */
    private $_P = array();


    function __construct(){
        $_POST['data'] = array(
            //'mid'=>'4',
            'name'=>'4',
            'content'=>'4',
            'present_id'=>'4',
            'rid'=>'4',
            'display'=>'4',
            'create_time'=>'4',
            'is_show'=>'4',
            'description'=>'4',
            'level'=>'4'            
            );
        $_POST['mid'] = 2;
        $_POST['rid'] = 10;
        $_POST['menuName'] = '4';
        $this->_P['data'] = $_POST['data'];
        $this->_P['mid'] = $_POST['mid'];
        $this->_P['rid'] = $_POST['rid'];
        $this->_P['menuName'] = $_POST['menuName'];
    }
    /**
     * 判断一个菜单是否存在
     * @param IN string $menuName 菜单名
     */
    public function existMenuName(){echo 'ok';exit;        
        $menuName = $this->_P['menuName'];        
        $this->_re = Mf_modelModel::_getRow($menuName);
        if(!empty($this->_re)){
            $this->_json(0,'存在');
        }else{
            $this->_json(1,'不存在');
        }
    }
    /**
     * 添加一个菜单
     * @param IN array $data 菜单详细数据
     */
    public function add(){echo 'ok';exit;
        $data = $this->_P['data'];
        if($this->_checkFields($data)){
            $check = Mf_modelModel::_getRow($data['name']);
            if(!empty($check)){
                $this->_json(1,'菜单已经存在');
            }
            $this->_re = Mf_modelModel::_add($data);
            if($this->_re){
                $this->_json(0,'添加菜单成功',$this->_re);
            }else{
                $this->_json(1,'添加菜单失败或已经添加');
            }
        }else{
            $this->_json(1,'字段校验失败');
        }
        
    }
    /**
     * 删除一个菜单
     * @param IN int $mid 删除一个菜单
     */
    public function del(){echo 'ok';exit;
        $data = array('is_show'=>'N');
        $mid = $this->_P['mid'];        
        $where = '`mid` = '.$mid.' limit 1 ';
        if($this->_re = Mf_modelModel::_update($data,'',$where)){
            $this->_json(0,'删除菜单成功');
        }else{
            $this->_json(1,'删除菜单失败或已经删除');
        }
    }
    /**
     * 修改一个菜单
     * @param IN array $data 菜单详细数据
     * @param IN int $mid 修改一个菜单
     */
    public function up(){echo '暂时不用';exit;
        $data = $this->_P['data'];
        $mid = $this->_P['mid'];
        $where = "`mid`=".$mid." limit 1 ";
        if($this->_checkFields($data)){
            $this->_re = Mf_modelModel::db()->update($data,'',$where);
            if($this->_re){
                $this->_json(0,'修改菜单成功');
            }else{
                $this->_json(1,'修改菜单失败或无修改数据');
            }
        }else{
            $this->_json(1,'字段校验失败');
        }
    }
    /**
     * 获取菜单的树形结构
     */
    public function getTreeMenu(){echo 'ok';exit;
        //$mid = $this->_P['mid'];
        $tree = '';
        $tree = $this->_getTree();
        if(!empty($tree)){
            $this->_json(0,'true',$tree);
        }else{
            $this->_json(1,'false');
        }
    }
    /**
     * 获取树形结构
     *
     * 查询出的无限级数组,数组结构必须包含arrar('id'=>'','parentid'=>'','name'=>'');与本身id，上一级id对应
     * @param IN int $id 当前选中id
     */
    private function _getTree($id = 0){
        $this->_re = Mf_modelModel::_getAll();
        /*树形结构处理开始*/
        foreach($this->_re as $key=>$value){//把无限极分类数组转换为属性结构对应的数组
            $this->_re[$key]['parentid'] = $value['present_id'];
            $this->_re[$key]['id'] = $value['mid'];
        }       
        $tree = new TreeLib();
        $tree->tree($this->_re);
       return '<select name="present_id">
        <option value=""></option>
        '. $tree->get_tree('0', "<option value='\$id' \$selected>\$spacer\$name</option>\n", $id).'
        </select>';
    }
    /**
     * 资源关联菜单
     * @param IN int $mid 菜单mid
     * @param IN int $rid 资源id
     */
    public function menuLinkResource(){echo 'ok';exit; 
        $rid = $this->_P['rid'];
        $mid = $this->_P['mid'];
        $data = array('rid'=>$rid);
        $where = "`mid`=".$mid." limit 1 ";
        if($this->_checkFields($data)){
            $this->_re = Mf_modelModel::_update($data,'',$where);
            if($this->_re){
                $this->_json(0,'资源关联菜单成功');
            }else{
                $this->_json(1,'资源关联菜单失败或无修改数据');
            }
        }else{
            $this->_json(1,'字段校验失败');
        }
    }
    /**
     * 获取1级菜单
     */
    public function getRootMenu(){echo 'ok';exit; 
        $this->_re = Mf_modelModel::_getAllByPid('0');
        if(!empty($this->_re)){
            $this->_json(0,true,$this->_re);
        }else{
            $this->_json(1,false);
        }
    }
    /**
     * 获取2级菜单
     * @param IN int $mid 菜单mid
     */
    public function getLeftMenu(){echo 'ok';exit; 
        $mid = $this->_P['mid'];
        $this->_re = Mf_modelModel::_getAllByPid($mid);
        if(!empty($this->_re)){
            $this->_json(0,true,$this->_re);
        }else{
            $this->_json(1,false);
        }
    }
    /**
     * 检查字段是否安全字段
     * @param IN array $data 数据
     * @param OUT boolen
     */
    private function _checkFields($data){
        foreach($data as $key=>$value){
            if(!in_array($key,$this->_fields)){
                return false;
            }
        }
        return true;
    }
    /**
     * json返回
     * @param IN string $err 错误代码
     * @param IN string $msg 提示信息
     * @param IN array $data 数据
     * @param OUT json
     */
    private function _json($err = 1, $msg = '联系管理员', $data = array()){
        $this->echoJson($err, $msg , $data , $this->_isJson );
    }
    function __destruct(){
        $this->_re = '';
        $this->_P = array();
    }
}